//
//  AppDelegate.m
//  YanMusicPlayer
//
//  Created by 1564335 on 15-6-11.
//  Copyright (c) 2015年 胡岩 All rights reserved.
//

#import "AppDelegate.h"
#import "SideBarViewController.h"
#import "Database.h"
#import <UMFeedback.h>
#import "GuideViewController.h"
@interface AppDelegate ()<show>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    NSLog(@"%@", NSHomeDirectory());
    [HearderView shareInstance].currentList = 0;
    sleep(2);
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
        GuideViewController *guide = [[GuideViewController alloc]init];
        self.window.rootViewController = guide;
        guide.myDelegate = self;
    } else {
        [self createMyView];
    }
    
    return YES;
}

- (void)show
{
    [self createMyView];
}

- (void)createMyView
{
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [UMFeedback setAppkey:@"5587587c67e58efb13004865"];
    
    SideBarViewController *sideBar = [[SideBarViewController alloc] init];
    self.window.rootViewController = sideBar;
    _view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _WIDTH, 20)];
    //    [self.window addSubview:_view];
    _view.backgroundColor = [UIColor whiteColor];
    
    _navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, _WIDTH, 54)];
    _navigationView.backgroundColor = [UIColor grayColor];
    //    [self.window addSubview:_navigationView];
    _navigationView.userInteractionEnabled = NO;
    
    if ([_USERDEFAULT objectForKey:@"number"] == nil) {
        NSString *str = @"1";
        [_USERDEFAULT setObject:@"first" forKey:@"first"];
        [_USERDEFAULT setObject:str forKey:@"number"];
        [_USERDEFAULT setObject:str forKey:@"tableCount"];
        [_USERDEFAULT setObject:str forKey:@"arrayCount"];
    }
    
    // 创建数据库
    [Database createTable];
    // 初始化喜欢歌单
    NSArray *arrayCollect = [Database selectData:@""];
    [[MusicPlayer1 shareInstance].collectList addObjectsFromArray:arrayCollect];
    NSArray *arrayDownload = [Database selectDownloadData];
    [[MusicPlayer1 shareInstance].downloadList addObjectsFromArray:arrayDownload];
    [[MusicPlayer1 shareInstance].list addObjectsFromArray:arrayCollect];
    
    NSArray *arrayCustom = [Database selectTableName];
    for (int i = 0; i < arrayCustom.count; i++){
        NameModel *model = [arrayCustom objectAtIndex:i];
        NSString *tableName = model.tableName;
        NSArray *getArray = [Database selectData:tableName];
        [[MusicPlayer1 shareInstance].mainList addObject:getArray];
    }
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    [super remoteControlReceivedWithEvent:event];
    if (event.type == UIEventTypeRemoteControl) {
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlPause:
                [[HearderView shareInstance] startMusic];
                break;
            case UIEventSubtypeRemoteControlPlay:
                [[HearderView shareInstance] startMusic];
                break;
            case UIEventSubtypeRemoteControlNextTrack:{
                [[HearderView shareInstance] nextMusic];
                break;}
            case UIEventSubtypeRemoteControlPreviousTrack:
                [[HearderView shareInstance] lastMusic];
                break;
                
            default:
                break;
        }
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [application beginBackgroundTaskWithExpirationHandler:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
